import 'package:cached_network_image/cached_network_image.dart';
import 'package:date_format/date_format.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mfkb/config/Config.dart';
import 'package:mfkb/pages/modules/CustomRoute.dart';
import 'package:mfkb/pages/school/RelationDetail.dart';
import 'package:mfkb/pages/web/WebViewByUrl.dart';
import 'package:mfkb/plugin/MyBehavior.dart';
import 'package:mfkb/service/DynamicService.dart';
import 'package:toast/toast.dart';

class Message extends StatefulWidget {
  @override
  _MessageState createState() => _MessageState();
}

class _MessageState extends State<Message> with SingleTickerProviderStateMixin {

  TabController mController;
  List tabList;
  List replys ;
  List ups ;
  List msg ;
  List _list ;

  @override
  void initState() {
    super.initState();
    initTabData();
    mController = TabController(
      length: tabList.length,
      vsync: this,
    );
    DynamicService().myReply().then((value){
      if(value["code"] == 200){
        replys = value["data"]["replys"] ;
        msg = value["data"]["msg"] ;
        _list = [] ;
        for(var i=0 ; i<replys.length ; i++){
          replys[i]["t"] = 1 ;
          _list.add(replys[i]) ;
        }
        for(var i = 0 ; i < msg.length ; i++){
          msg[i]["t"] = 2 ;
          _list.add(msg[i]) ;
        }
        _list.sort((a , b){
          return b["time"] - a["time"] ;
        }) ;
        setState(() {});
      }else{
        Toast.show(value["msg"], context) ;
      }
    }) ;
    DynamicService().myUps().then((value){
      //print(value) ;
      if(value["code"] == 200){
        ups = value["data"] ;
        setState(() {});
      }else{
        Toast.show(value["msg"], context) ;
      }
    }) ;
  }

  initTabData() {
    tabList = [
      TabTitle("评论" , 1) , TabTitle("点赞" , 2)
    ];
  }

  @override
  void dispose() {
    super.dispose();
    mController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: SystemUiOverlayStyle.dark ,
      child: Scaffold(
        body: Container(
          color: Colors.white ,
          child: Column(
            children: <Widget>[
              Container(
                height: MediaQuery.of(context).padding.top ,
              ) ,
              Container(
                height: 45 ,
                // color: Colors.red ,
                child: Row(
                  children: <Widget>[
                    GestureDetector(
                      child: Container(
                        color: Color.fromARGB(0, 0, 0, 0),
                        width: ScreenUtil().setWidth(36),
                        child: Container(
                          width: ScreenUtil().setWidth(17),
                          height: ScreenUtil().setWidth(17),
                          margin: EdgeInsets.only(left:ScreenUtil().setWidth(19)),
                          child: Image.asset("assets/images/back.png" , fit:BoxFit.fill),
                        ),
                      ),
                      onTap: (){
                        Navigator.of(context).pop() ;
                      },
                    ) ,
                    Expanded(
                      child: Center(
                        child: TabBar(
                          isScrollable: true,
                          //是否可以滚动
                          controller: mController,
                          labelColor: Color.fromARGB(255, 19, 28, 53),
                          unselectedLabelColor: Color.fromARGB(255, 121, 132, 155),
                          labelStyle: TextStyle(
                            fontSize: ScreenUtil().setSp(18) ,
                            fontWeight: FontWeight.bold
                          ),
                          unselectedLabelStyle: TextStyle(
                            fontSize: ScreenUtil().setSp(16)
                          ),
                          indicatorWeight: 2.2 ,
                          indicatorColor: Color.fromARGB(255, 0, 195, 206),
                          indicatorSize: TabBarIndicatorSize.label,
                          tabs: tabList.map((item) {
                            return Tab(
                              text:item.title,
                            );
                          }).toList(),
                        ),
                      ),
                    ) ,
                    Container(
                      width: ScreenUtil().setWidth(36),
                    )
                  ],
                ),
              ) ,
              Expanded(
                child: Container(
                  child: TabBarView(
                    controller: mController,
                    children: tabList.map((item) {
                      return Stack(children: <Widget>[
                        Align(
                          alignment:Alignment.topCenter,
                          child: item.getTab() == 1 ? Container(
                            child: ScrollConfiguration(
                              behavior: MyBehavior() ,
                              child: ListView(
                                padding: EdgeInsets.all(0) ,
                                children: <Widget>[
                                  Column(
                                    children: List.generate(_list == null ? 0 : _list.length, (index){
                                      return GestureDetector(
                                        child: Container(
                                          height: ScreenUtil().setHeight(80),
                                          color: Color.fromARGB(0, 0, 0, 0),
                                          child: Row(
                                            children: <Widget>[
                                              Container(
                                                width: ScreenUtil().setHeight(50),
                                                height: ScreenUtil().setHeight(50),
                                                margin: EdgeInsets.only(left:ScreenUtil().setWidth(15)),
                                                child: ClipRRect(
                                                  borderRadius: BorderRadius.all(Radius.circular(ScreenUtil().setHeight(50))),
                                                  child: CachedNetworkImage(
                                                    imageUrl: Config().getImgHost() + (_list[index]["t"] == 1 ? _list[index]["user"]["icon"] : _list[index]["icon"]),
                                                    placeholder: (c , u){
                                                      return (
                                                        Container(
                                                          color: Color.fromARGB(255, 246, 245, 248),
                                                          child: Center(
                                                            child: Image.asset("assets/images/imageloading.png"),
                                                          ),
                                                        )
                                                      ) ;
                                                    },
                                                  ),
                                                ),
                                              ) , 
                                              Expanded(
                                                child: Container(
                                                  height: ScreenUtil().setHeight(42) ,
                                                  margin: EdgeInsets.only(left:ScreenUtil().setWidth(12)),
                                                  child: Column(
                                                    crossAxisAlignment: CrossAxisAlignment.start ,
                                                    children: <Widget>[
                                                      Container(
                                                        height: ScreenUtil().setHeight(20),
                                                        child: Row(
                                                          children: <Widget>[
                                                            Container(
                                                              child: Text(_list[index]["t"] == 1 ? _list[index]["user"]["nickname"] :"系统通知" , style: TextStyle(
                                                                fontSize: ScreenUtil().setSp(16) ,
                                                                color: Color.fromARGB(255, 19, 28, 53) ,
                                                                fontWeight: FontWeight.bold ,
                                                                height: ScreenUtil().setHeight(20) / ScreenUtil().setSp(16)
                                                              ),),
                                                            ) ,
                                                            Visibility(
                                                              visible: _list[index]["t"] == 1,
                                                              child: Container(
                                                                margin: EdgeInsets.only(left:ScreenUtil().setWidth(10)),
                                                                child: Text("评论了你" , style: TextStyle(
                                                                  fontSize: ScreenUtil().setSp(12) ,
                                                                  color: Color.fromARGB(255, 121, 132, 155) ,
                                                                  height: ScreenUtil().setHeight(15) / ScreenUtil().setSp(12)
                                                                ),),
                                                              ),
                                                            ) ,
                                                            Expanded(
                                                              child:Container(
                                                                margin: EdgeInsets.only(right:ScreenUtil().setWidth(15)),
                                                                child: Text(formatDate(DateTime.fromMillisecondsSinceEpoch(_list[index]["time"]), ["yyyy" , "-" , "mm" , "-" , "dd"]) , textAlign: TextAlign.end,style: TextStyle(
                                                                  fontSize: ScreenUtil().setSp(12) ,
                                                                  color: Color.fromARGB(255, 121, 132, 155)
                                                                ),),
                                                              )
                                                            )
                                                          ],
                                                        ),
                                                      ) ,
                                                      Container(
                                                        height: ScreenUtil().setHeight(15),
                                                        margin: EdgeInsets.only(top:ScreenUtil().setHeight(6)),
                                                        child: Text(_list[index]["t"] == 1 ? _list[index]["content"] : _list[index]["subTitle"] , overflow: TextOverflow.ellipsis,style: TextStyle(
                                                          fontSize: ScreenUtil().setSp(12) ,
                                                          color: Color.fromARGB(255, 78, 86, 108) ,
                                                          height: ScreenUtil().setHeight(15) / ScreenUtil().setSp(12)
                                                        ),),
                                                      )
                                                    ],
                                                  ),
                                                ),
                                              )
                                            ],
                                          ),
                                        ),
                                        onTap: (){
                                          if(_list[index]["t"] == 2){
                                            if(_list[index]["type"] == 1){
                                              Navigator.of(context).push(CustomRoute(WebViewByUrl(url: _list[index]["file"]))) ;
                                            }
                                          }else if(_list[index]["t"] == 1){
                                            Navigator.of(context).push(CustomRoute(RelationDetail(id: replys[index]["did"]))) ;
                                          }
                                        },
                                      ) ;
                                    }),
                                  ) ,
                                  // Column(
                                  //   children: List.generate(replys == null ? 0 : replys.length, (index){
                                  //     return GestureDetector(
                                  //       child: Container(
                                  //         height: ScreenUtil().setHeight(80),
                                  //         color: Color.fromARGB(0, 0, 0, 0),
                                  //         child: Row(
                                  //           children: <Widget>[
                                  //             Container(
                                  //               width: ScreenUtil().setHeight(50),
                                  //               height: ScreenUtil().setHeight(50),
                                  //               margin: EdgeInsets.only(left:ScreenUtil().setWidth(15)),
                                  //               child: ClipRRect(
                                  //                 borderRadius: BorderRadius.all(Radius.circular(ScreenUtil().setHeight(50))),
                                  //                 child: CachedNetworkImage(
                                  //                   imageUrl: Config().getImgHost() + replys[index]["user"]["icon"],
                                  //                   placeholder: (c , u){
                                  //                     return (
                                  //                       Container(
                                  //                         color: Color.fromARGB(255, 246, 245, 248),
                                  //                         child: Center(
                                  //                           child: Image.asset("assets/images/imageloading.png"),
                                  //                         ),
                                  //                       )
                                  //                     ) ;
                                  //                   },
                                  //                 ),
                                  //               ),
                                  //             ) , 
                                  //             Expanded(
                                  //               child: Container(
                                  //                 height: ScreenUtil().setHeight(42) ,
                                  //                 margin: EdgeInsets.only(left:ScreenUtil().setWidth(12)),
                                  //                 child: Column(
                                  //                   crossAxisAlignment: CrossAxisAlignment.start ,
                                  //                   children: <Widget>[
                                  //                     Container(
                                  //                       height: ScreenUtil().setHeight(20),
                                  //                       child: Row(
                                  //                         children: <Widget>[
                                  //                           Container(
                                  //                             child: Text(replys[index]["user"]["nickname"] , style: TextStyle(
                                  //                               fontSize: ScreenUtil().setSp(16) ,
                                  //                               color: Color.fromARGB(255, 19, 28, 53) ,
                                  //                               fontWeight: FontWeight.bold ,
                                  //                               height: ScreenUtil().setHeight(20) / ScreenUtil().setSp(16)
                                  //                             ),),
                                  //                           ) ,
                                  //                           Container(
                                  //                             margin: EdgeInsets.only(left:ScreenUtil().setWidth(10)),
                                  //                             child: Text("评论了你" , style: TextStyle(
                                  //                               fontSize: ScreenUtil().setSp(12) ,
                                  //                               color: Color.fromARGB(255, 121, 132, 155) ,
                                  //                               height: ScreenUtil().setHeight(15) / ScreenUtil().setSp(12)
                                  //                             ),),
                                  //                           ) ,
                                  //                           Expanded(
                                  //                             child:Container(
                                  //                               margin: EdgeInsets.only(right:ScreenUtil().setWidth(15)),
                                  //                               child: Text(formatDate(DateTime.fromMillisecondsSinceEpoch(replys[index]["time"]), ["yyyy" , "-" , "mm" , "-" , "dd"]) , textAlign: TextAlign.end,style: TextStyle(
                                  //                                 fontSize: ScreenUtil().setSp(12) ,
                                  //                                 color: Color.fromARGB(255, 121, 132, 155)
                                  //                               ),),
                                  //                             )
                                  //                           )
                                  //                         ],
                                  //                       ),
                                  //                     ) ,
                                  //                     Container(
                                  //                       height: ScreenUtil().setHeight(15),
                                  //                       margin: EdgeInsets.only(top:ScreenUtil().setHeight(6)),
                                  //                       child: Text(replys[index]["content"] , overflow: TextOverflow.ellipsis,style: TextStyle(
                                  //                         fontSize: ScreenUtil().setSp(12) ,
                                  //                         color: Color.fromARGB(255, 78, 86, 108) ,
                                  //                         height: ScreenUtil().setHeight(15) / ScreenUtil().setSp(12)
                                  //                       ),),
                                  //                     )
                                  //                   ],
                                  //                 ),
                                  //               ),
                                  //             )
                                  //           ],
                                  //         ),
                                  //       ),
                                  //       onTap: (){
                                  //         Navigator.of(context).push(CustomRoute(RelationDetail(id: replys[index]["did"]))) ;
                                  //       },
                                  //     ) ;
                                  //   }),
                                  // )
                                ],
                              ) ,
                            ) ,
                          ) : Container(
                            child: ScrollConfiguration(
                              behavior: MyBehavior() ,
                              child: ListView(
                                padding: EdgeInsets.all(0) ,
                                children: List.generate(ups == null ? 0 : ups.length, (index){
                                  return GestureDetector(
                                    child: Container(
                                      height: ScreenUtil().setHeight(80),
                                      color: Color.fromARGB(0, 0, 0, 0),
                                      child: Row(
                                        children: <Widget>[
                                          Container(
                                            width: ScreenUtil().setHeight(50),
                                            height: ScreenUtil().setHeight(50),
                                            margin: EdgeInsets.only(left:ScreenUtil().setWidth(15)),
                                            child: ClipRRect(
                                              borderRadius: BorderRadius.all(Radius.circular(ScreenUtil().setHeight(50))),
                                              child: CachedNetworkImage(
                                                imageUrl: Config().getImgHost() + ups[index]["user"]["icon"],
                                                placeholder: (c , u){
                                                  return (
                                                    Container(
                                                      color: Color.fromARGB(255, 246, 245, 248),
                                                      child: Center(
                                                        child: Image.asset("assets/images/imageloading.png"),
                                                      ),
                                                    )
                                                  ) ;
                                                },
                                              ),
                                            ),
                                          ) , 
                                          Expanded(
                                            child: Container(
                                              height: ScreenUtil().setHeight(42) ,
                                              margin: EdgeInsets.only(left:ScreenUtil().setWidth(12)),
                                              child: Column(
                                                crossAxisAlignment: CrossAxisAlignment.start ,
                                                children: <Widget>[
                                                  Container(
                                                    height: ScreenUtil().setHeight(20),
                                                    child: Row(
                                                      children: <Widget>[
                                                        Container(
                                                          child: Text(ups[index]["user"]["nickname"] , style: TextStyle(
                                                            fontSize: ScreenUtil().setSp(16) ,
                                                            color: Color.fromARGB(255, 19, 28, 53) ,
                                                            fontWeight: FontWeight.bold ,
                                                            height: ScreenUtil().setHeight(20) / ScreenUtil().setSp(16)
                                                          ),),
                                                        ) ,
                                                        Container(
                                                          margin: EdgeInsets.only(left:ScreenUtil().setWidth(10)),
                                                          child: Text("给你点赞" , style: TextStyle(
                                                            fontSize: ScreenUtil().setSp(12) ,
                                                            color: Color.fromARGB(255, 121, 132, 155) ,
                                                            height: ScreenUtil().setHeight(15) / ScreenUtil().setSp(12)
                                                          ),),
                                                        ) ,
                                                        Expanded(
                                                          child:Container(
                                                            margin: EdgeInsets.only(right:ScreenUtil().setWidth(15)),
                                                            child: Text(ups[index]["time"] , textAlign: TextAlign.end,style: TextStyle(
                                                              fontSize: ScreenUtil().setSp(12) ,
                                                              color: Color.fromARGB(255, 121, 132, 155)
                                                            ),),
                                                          )
                                                        )
                                                      ],
                                                    ),
                                                  ) ,
                                                  Container(
                                                    height: ScreenUtil().setHeight(15),
                                                    margin: EdgeInsets.only(top:ScreenUtil().setHeight(6)),
                                                    child: Text("赞了你的发布（"+ups[index]["content"]+"）" , overflow: TextOverflow.ellipsis,style: TextStyle(
                                                      fontSize: ScreenUtil().setSp(12) ,
                                                      color: Color.fromARGB(255, 78, 86, 108) ,
                                                      height: ScreenUtil().setHeight(15) / ScreenUtil().setSp(12)
                                                    ),),
                                                  )
                                                ],
                                              ),
                                            ),
                                          )
                                        ],
                                      ),
                                    ),
                                    onTap: (){
                                      Navigator.of(context).push(CustomRoute(RelationDetail(id: ups[index]["did"]))) ;
                                    },
                                  ) ;
                                }),
                              ) ,
                            )
                          ) 
                        ),
                      ],);
                    }).toList(),
                  ),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}

class TabTitle{
  String title ;
  int tab ;
  
  TabTitle(@required this.title , @required this.tab) ;

  getTitle(){
    return this.title ;
  }

  getTab(){
    return this.tab ;
  }
}